package com.chaoyi.controller;

import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.chaoyi.model.Appointment;
import com.chaoyi.service.AppointmentService;
import com.chaoyi.util.upload.FileType;
import com.chaoyi.util.upload.UploadRes;

/**
 * 秘约功能
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/appointment")
public class AppointmentController {
	@Resource
	private AppointmentService appointmentService;
	
	/**
	 * 初始化跳转页面
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/list")
	public ModelAndView list(HttpServletRequest request,HttpServletResponse response){
		ModelAndView mod =new ModelAndView("/appointment/appointmentList");
		return mod;
	}
	
	/**
	 * 首页展示
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("appointmentList")
	@ResponseBody
	public JSONObject appointmentList(HttpServletRequest request, HttpServletResponse response) {
		JSONObject json = new JSONObject();
		String offset = request.getParameter("offset");
		String limit = request.getParameter("limit");
		List<Appointment> list = appointmentService.findList(offset, limit);
		int count =appointmentService.findCount();
		json.put("rows", list);
		json.put("total", count);
		return json;
	}
	
	/**
	 *  新增跳转页面
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("addAppointment_jump")
	public ModelAndView addAppointment_jump(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView mod = new ModelAndView("/appointment/addAppointment");
		return mod;
	}
	/**
	 * 保存新增内容
	 */
	@ResponseBody
	@RequestMapping("addAppointment")
	public JSONObject addAppointment(HttpServletRequest request, HttpServletResponse response, Appointment appointment) {
		JSONObject json = new JSONObject();
		int number = appointmentService.saveAppointment(appointment);
		if (number == 1) {
			json.put("missage", "success");
		} else {
			json.put("missage", "error");
		}
		return json;
	}
	
	/**
	 * 修改内容跳转
	 */
	@RequestMapping("/alertAppointment")
	public ModelAndView alertHome(HttpServletRequest request, HttpServletResponse response,int id) {
		ModelAndView mod = new ModelAndView("/appointment/alertAppointment");
		Appointment appointment = appointmentService.findAppointmentOne(id);
		mod.addObject("obj",appointment);
		return mod;
	}
	
	/**
	 * 保存修改的信息
	 */
	@ResponseBody
	@RequestMapping("alertAppointmentSave")
	public JSONObject alertAppointmentSave(HttpServletRequest request, HttpServletResponse response, Appointment appointment) {
		JSONObject json = new JSONObject();
		int number = appointmentService.alertAppointmentSave(appointment);
		if (number == 1) {
			json.put("missage", "success");
		} else {
			json.put("missage", "error");
		}
		return json;
	}
	
	/**
	 * 删除首页内容
	 */
	@ResponseBody
	@RequestMapping("deleteAppointment")
	public JSONObject deleteAppointment(HttpServletRequest request, HttpServletResponse response, int id) {
		JSONObject json = new JSONObject();
		int number = appointmentService.deleteAppointment(id);
		if (number == 1) {
			json.put("missage", "success");
		} else {
			json.put("missage", "error");
		}
		return json;
	}
	/**
	 * 头像上传
	 * @param response
	 * @param uploadFile
	 * @return
	 * @throws IOException
	 */
	@RequestMapping("/uploadClassifyImg")
	@ResponseBody
	public JSONObject uploadClassifyImg(HttpServletResponse response,@RequestParam("file_data")Part uploadFile) throws IOException{
		String imgUrl = null;
		try{
			imgUrl = appointmentService.uploadClassifyImg2WSCDN(uploadFile);
		}catch(Exception e){
			return UploadRes.error("文件上传失败");
		}
		JSONObject result = UploadRes.success()
				.setFileUrl(imgUrl)
				.setFileType(FileType.IMAGE)
				.setDelelteUrl("deletefile")
				.putExtraParam("path", imgUrl).toJSON();
		return result;
	}
}
